/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.wxrobot.service;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeesite.common.entity.Page;
import com.jeesite.common.service.CrudService;
import com.jeesite.modules.wxrobot.entity.TaskActivity;
import com.jeesite.modules.wxrobot.dao.TaskActivityDao;
import com.jeesite.modules.file.utils.FileUploadUtils;

/**
 * 任务宝活动Service
 * @author sam
 * @version 2018-08-08
 */
@Service
@Transactional(readOnly=true)
public class TaskActivityService extends CrudService<TaskActivityDao, TaskActivity> {
	
	/**
	 * 获取单条数据
	 * @param taskActivity
	 * @return
	 */
	@Override
	public TaskActivity get(TaskActivity taskActivity) {
		return super.get(taskActivity);
	}
	
	/**
	 * 查询分页数据
	 * @param page 分页对象
	 * @param taskActivity
	 * @return
	 */
	@Override
	public Page<TaskActivity> findPage(Page<TaskActivity> page, TaskActivity taskActivity) {
		return super.findPage(page, taskActivity);
	}
	
	/**
	 * 保存数据（插入或更新）
	 * @param taskActivity
	 */
	@Override
	@Transactional(readOnly=false)
	public void save(TaskActivity taskActivity) {
		super.save(taskActivity);
		// 保存上传图片
		FileUploadUtils.saveFileUpload(taskActivity.getId(), "taskActivity_image");
		// 保存好友应答图片
		FileUploadUtils.saveFileUpload(taskActivity.getId(), "friendsPic_image");
		// 保存正确邀请码图片
		FileUploadUtils.saveFileUpload(taskActivity.getId(), "rightCodePic_image");
		// 保存错误邀请码图片
		FileUploadUtils.saveFileUpload(taskActivity.getId(), "errorCodePic_image");
		// 保存助力成功图片
		FileUploadUtils.saveFileUpload(taskActivity.getId(), "helpsArrivalPic_image");
	}
	
	/**
	 * 更新状态
	 * @param taskActivity
	 */
	@Override
	@Transactional(readOnly=false)
	public void updateStatus(TaskActivity taskActivity) {
		super.updateStatus(taskActivity);
	}
	
	/**
	 * 删除数据
	 * @param taskActivity
	 */
	@Override
	@Transactional(readOnly=false)
	public void delete(TaskActivity taskActivity) {
		super.delete(taskActivity);
	}

	public TaskActivity getByRobotWeixin(String robotWeixinUserName) {
		TaskActivity taskActivity = new TaskActivity();
		taskActivity.setRobotWeixinUserName(robotWeixinUserName);
		return this.dao.getByRobotWeixin(taskActivity);
	}

	@Transactional(readOnly=false)
	public void updateStatusAndRobotPoster(TaskActivity taskActivity) {
		dao.updateStatusAndRobotPoster(taskActivity);
	}

	@Transactional(readOnly=false)
	public void offlineRobot(String robotWeixinUserName) {
		this.dao.offlineRobot(robotWeixinUserName);
	}
	/**
	 * 上线机器人
	 * @param activityId 活动id
	 * @param robotWeixinUserName 机器人微信用户名
	 */
	@Transactional(readOnly=false)
	public void onlineRobot(String activityId, String robotWeixinUserName) {
		//下线微信对应的其他机器人
		this.dao.offlineRobot(robotWeixinUserName);
		//上线当前活动机器人
		this.dao.onlineActivityRobot(activityId, robotWeixinUserName);
	}

}